package cn.bandeqing.pay.gnete.upbc.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.security.PrivateKey;
import java.security.PublicKey;

public class TestSecurity {

	public static String defaultPass = "123456";
	public static String pubPath = "ogw-dev-2048-cfca.der";
	public static String pfxPath = "mer-dev-A0010000_I-0002.p12";

	/**
	 * 从私钥证书获取16进制的私钥串、base64加密后的私钥串
	 */
//	@Test
	public void getPrivateKeyByPfx() throws Exception {
		PrivateKey privKey = PFXUtil.getPrivateKeyByPfx(pfxPath, defaultPass);
		byte[] priKeyByte = privKey.getEncoded();

		// 16进制的私钥串
		String priKeyHex = Hex.encodeHexString(priKeyByte);
		System.out.println("priKeyHex:" + priKeyHex);

		// base64加密后的私钥串
		String base64Crypt = new String(Base64.encodeBase64(priKeyByte), "UTF-8");
		System.out.println("base64Crypt:" + base64Crypt);
		 
	}

	/**
	 * 从公钥证书获取16进制的公钥串
	 */
//	@Test
	public void getPubKeyByPfx() throws Exception {
		PublicKey publicKey = PFXUtil.getPubKeyByPfxByPfx(pubPath);

		// 16进制的公钥串
		String priKeyHex = Hex.encodeHexString(publicKey.getEncoded());
		System.out.println("publicKey:" + priKeyHex);
	}
}
